package com.edu.exam.entity.curriculum.questionbank;

import com.edu.exam.entity.curriculum.classification.QuestionType;
import com.edu.exam.enums.exam.QuestionStatus;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.Comment;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;

import jakarta.persistence.*;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 试题实体类
 * 所属模块：B-02 题库管理模块
 * 对应数据库表：questions
 *
 * 管理试题的基本信息，包括试题编码、内容、类型、难度、分值等。
 * 支持试题的创建、编辑、审核、发布等全生命周期管理。
 * 关联题库、分类、认知层次等关键信息，提供试题的完整视图。
 *
 * @author System
 * @version 1.0.0
 */
@Entity
@Comment("试题基础信息表")
@SQLDelete(sql = "UPDATE questions SET is_deleted = true, delete_time = CURRENT_TIMESTAMP WHERE id = ?")
@Where(clause = "is_deleted = false")
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = false)
public class Question {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @NotBlank(message = "试题编码不能为空")
    @Size(max = 50, message = "试题编码长度不能超过50个字符")
    @Column(name = "question_code", nullable = false, length = 50, unique = true)
    private String questionCode;

    @NotNull(message = "题库ID不能为空")
    @Column(name = "question_bank_id", nullable = false)
    private Long questionBankId;

    @NotNull(message = "试题类型不能为空")
    @Enumerated(EnumType.STRING)
    @Column(name = "question_type", nullable = false, length = 30)
    private QuestionType questionType;

    @NotNull(message = "试题内容不能为空")
    @Column(name = "question_content", nullable = false, columnDefinition = "TEXT")
    private String questionContent;

    @Column(name = "question_options", columnDefinition = "TEXT")
    private String questionOptions;

    @Column(name = "correct_answer", columnDefinition = "TEXT")
    private String correctAnswer;

    @Column(name = "answer_explanation", columnDefinition = "TEXT")
    private String answerExplanation;

    @NotNull(message = "分值不能为空")
    @Column(name = "points", nullable = false)
    private BigDecimal points;

    @NotNull(message = "难度级别不能为空")
    @Column(name = "difficulty_level", nullable = false)
    private Integer difficultyLevel;

    @NotNull(message = "试题状态不能为空")
    @Enumerated(EnumType.STRING)
    @Column(name = "question_status", nullable = false, length = 20)
    private QuestionStatus questionStatus;

    @Column(name = "subject_id")
    private Long subjectId;

    @Column(name = "subject_name", length = 100)
    private String subjectName;

    @Column(name = "knowledge_points", length = 1000)
    private String knowledgePoints;

    @Column(name = "cognitive_level", length = 50)
    private String cognitiveLevel;

    @Column(name = "estimated_time")
    private Integer estimatedTime; // 预估答题时间（秒）

    @Column(name = "tags", length = 500)
    private String tags;

    @Column(name = "keywords", length = 500)
    private String keywords;

    @Column(name = "reference_materials", length = 1000)
    private String referenceMaterials;

    @Column(name = "is_multiple_correct")
    @Builder.Default
    private Boolean isMultipleCorrect = false;

    @Column(name = "randomize_options")
    @Builder.Default
    private Boolean randomizeOptions = false;

    @Column(name = "requires_manual_grading")
    @Builder.Default
    private Boolean requiresManualGrading = false;

    @Column(name = "usage_count")
    @Builder.Default
    private Integer usageCount = 0;

    @Column(name = "correct_rate")
    private BigDecimal correctRate;

    @Column(name = "average_score")
    private BigDecimal averageScore;

    @Version
    @Column(name = "version", nullable = false)
    @Builder.Default
    private Integer version = 1;

    @Column(name = "is_deleted", nullable = false)
    @Builder.Default
    private Boolean isDeleted = false;

    @Column(name = "delete_time")
    private LocalDateTime deleteTime;

    @Size(max = 50, message = "创建人长度不能超过50个字符")
    @Column(name = "created_by", nullable = false, length = 50)
    private String createdBy;

    @Column(name = "created_time", nullable = false, updatable = false)
    private LocalDateTime createdTime;

    @Size(max = 50, message = "更新人长度不能超过50个字符")
    @Column(name = "updated_by", length = 50)
    private String updatedBy;

    @Column(name = "updated_time", nullable = false)
    private LocalDateTime updatedTime;

    // 关联关系
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "question_bank_id", insertable = false, updatable = false)
    private QuestionBank questionBank;

    @OneToMany(mappedBy = "question", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    private List<QuestionAnalysis> analyses;

    @OneToMany(mappedBy = "question", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    private List<QuestionHistory> history;

}

